package com.geeklei.media.ui.controller;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Handler;
import android.provider.Settings.System;
import android.util.Log;
import android.view.OrientationEventListener;

public class OrientationUpdater implements LifeCycle {
    public static final String TAG = "OrientationUpdater";
    private Activity mActivity;
    private int mActivityOrientation;
    private OrientationEventListener mOrientationEventListener;
    private final OrientationRequest mOrientationRequest;
    private int mRequestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
    private boolean mResponse2Event = true;
    private boolean mUserLockedScreen = false;
    private int mWaitForNextOrientation = Integer.MAX_VALUE;
    private boolean enablePortraint = true;
    private class OrientationRequest implements Runnable {
        private int mTarget;
        private final Handler mUpdateHandler;

        OrientationRequest(Activity activity) {
            this.mUpdateHandler = new Handler(activity.getMainLooper());
        }

        public void change(int i) {
            if (this.mTarget != i) {
                if(!enablePortraint){
                    if(this.mTarget == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                            || this.mTarget == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE){
                        this.mTarget = i;
                        this.mUpdateHandler.removeCallbacks(this);
                        this.mUpdateHandler.postDelayed(this, 1500);
                    }
                }else{
                    this.mTarget = i;
                    this.mUpdateHandler.removeCallbacks(this);
                    this.mUpdateHandler.postDelayed(this, 1500);
                }
            }
        }

        public void cancle() {
            this.mUpdateHandler.removeCallbacks(this);
        }

        public void run() {
            if (OrientationUpdater.this.mResponse2Event && OrientationUpdater.this.mActivity.getRequestedOrientation() != this.mTarget) {
                OrientationUpdater.this.mActivity.setRequestedOrientation(this.mTarget);
            }
        }
    }

    public OrientationUpdater(Activity activity) {
        this.mActivity = activity;
        this.mActivityOrientation = this.mActivity.getResources().getConfiguration().orientation;
        this.mOrientationRequest = new OrientationRequest(activity);
        initListener();
    }

    private void initListener() {
        this.mOrientationEventListener = new OrientationEventListener(this.mActivity) {
            public void onOrientationChanged(int i) {
                if (i != -1 && !OrientationUpdater.this.mUserLockedScreen && !OrientationUpdater.isSystemRotationLocked(OrientationUpdater.this.mActivity)) {
                    OrientationUpdater.this.handleOrientationChange(i);
                }
            }
        };
    }

    private void handleOrientationChange(int i) {
        updateOrientation();
        if (!isSensorOrientation()) {
            int caculateOrientation = caculateOrientation(i);
            if (this.mWaitForNextOrientation == Integer.MAX_VALUE || caculateOrientation == this.mWaitForNextOrientation) {
                this.mWaitForNextOrientation = Integer.MAX_VALUE;
                if (caculateOrientation != -1) {
                    this.mOrientationRequest.change(caculateOrientation);
                }
            }
        }
    }

    private int caculateOrientation(int i) {
        if ((i >= 0 && i < 45) || (i >= 315 && i < 360)) {
            return 1;
        }
        if (i >= 45 && i < 135) {
            return 8;
        }
        if (i >= 135 && i < 225) {
            return 9;
        }
        if (i < 225 || i >= 315) {
            return -1;
        }
        return 0;
    }

    private boolean isSensorOrientation() {
        return this.mActivity.getRequestedOrientation() == 4;
    }

    public void onConfigurationChanged(Configuration configuration) {
        Log.d(TAG, "onConfigurationChanged: " + configuration);
        updateOrientation();
    }

    private void updateOrientation() {
        int i = this.mActivity.getResources().getConfiguration().orientation;
        if (this.mActivityOrientation != i) {
            this.mActivityOrientation = i;
            onOrientationChanged();
        }
    }

    private int getScreenOrientation() {
        int rotation;
        if (this.mActivity != null) {
            rotation = this.mActivity.getWindowManager().getDefaultDisplay().getRotation();
        } else {
            rotation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        }
        if (this.mActivityOrientation ==  ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            if (rotation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            }
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        } else if (rotation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
        } else {
            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        }
    }

    private void onOrientationChanged() {
    }

    public void requestLandscape() {
        this.mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        this.mWaitForNextOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    }

    public void requestPortrait() {
        this.mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        this.mWaitForNextOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }

    public void disableRotation() {
        this.mUserLockedScreen = true;
        this.mRequestedOrientation = this.mActivity.getRequestedOrientation();
        this.mActivity.setRequestedOrientation(getScreenOrientation());
    }

    public void enableRotation() {
        this.mUserLockedScreen = false;
        this.mActivity.setRequestedOrientation(this.mRequestedOrientation);
    }

    public static boolean isSystemRotationLocked(Context context) {
        return System.getInt(context.getContentResolver(), "accelerometer_rotation", 0) == 0;
    }

    public void onCreate() {
    }

    public void onStart() {
        this.mOrientationEventListener.enable();
    }

    public void onResume() {
    }

    public void onPause() {
    }

    public void onStop() {
        this.mOrientationEventListener.disable();
        if (this.mOrientationRequest != null) {
            this.mOrientationRequest.cancle();
        }
    }

    public void onDestroy() {
    }

    public void stop() {
        this.mResponse2Event = false;
    }

    public void start() {
        this.mResponse2Event = true;
    }

    public boolean isEnablePortraint() {
        return enablePortraint;
    }

    public void setEnablePortraint(boolean enablePortraint) {
        this.enablePortraint = enablePortraint;
    }
}
